package org.musicsweet.service;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.musicsweet.constant.StringConstant;
import org.musicsweet.entity.SysDicItem;
import org.musicsweet.entity.SysUser;
import org.musicsweet.mapper.SysDicItemMapper;
import org.musicsweet.result.ResultData;
import org.musicsweet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 系统字典项Service
 */
@Slf4j
@Service
public class SysDicItemService {

    @Autowired
    private SysDicItemMapper sysDicItemMapper;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 根据id查询系统字典项
     *
     * @param sysDicItem
     * @return
     */
    public ResultData<SysDicItem> getSysDicItemById(SysDicItem sysDicItem) {
        log.info("根据id查询系统字典项:"+ JSON.toJSONString(sysDicItem));
        if(StringUtils.isEmpty(sysDicItem.getId())){
            return ResultData.createFail("id不能为空");
        }
        sysDicItem.setDel(StringConstant.DEL_NO);
        SysDicItem data = sysDicItemMapper.getSysDicItemById(sysDicItem);
        return ResultData.successData("查询成功",data);
    }

    /**
     * 多条件查询系统字典项
     *
     * @param sysDicItem
     * @return
     */
    public ResultData<List<SysDicItem>> getSysDicItemList(SysDicItem sysDicItem) {
        log.info("多条件查询系统字典项:"+ JSON.toJSONString(sysDicItem));
        sysDicItem.setDel(StringConstant.DEL_NO);
        List<SysDicItem> data = sysDicItemMapper.getSysDicItemList(sysDicItem);
        return ResultData.successData("查询成功",data);
    }


    /**
     * 添加系统字典项
     * @param sysDicItem
     * @param token
     * @return
     */
    @Transactional
    public ResultData addSysDicItem(SysDicItem sysDicItem,String token) {
        sysDicItem.setId(StringUtil.getUUID());
        sysDicItem.setCreateTime(new Date());
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        sysDicItem.setCreateUserId(currentUser.getId());
        sysDicItem.setDel(StringConstant.DEL_NO);
        log.info("添加系统字典项:"+ JSON.toJSONString(sysDicItem));
        int i = sysDicItemMapper.addSysDicItem(sysDicItem);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("添加系统字典项失败");
        }
    }


    /**
     * 修改系统字典项
     *
     * @param sysDicItem
     * @return
     */
    @Transactional
    public ResultData updateSysDicItem(SysDicItem sysDicItem,String token) {
        if(StringUtils.isEmpty(sysDicItem.getId())){
            return ResultData.createFail("id不能为空");
        }
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        sysDicItem.setUpdateUserId(currentUser.getId());
        sysDicItem.setUpdateTime(new Date());
        log.info("修改系统字典项:"+ JSON.toJSONString(sysDicItem));
        int i = sysDicItemMapper.updateSysDicItem(sysDicItem);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("修改系统字典项失败");
        }
    }


    /**
     * 删除系统字典项
     *
     * @param sysDicItem
     * @return
     */
    @Transactional
    public ResultData delSysDicItem(SysDicItem sysDicItem) {
        if(StringUtils.isEmpty(sysDicItem.getId())){
            return ResultData.createFail("id不能为空");
        }
        sysDicItem.setDel(StringConstant.DEL_OK);
        log.info("删除系统字典项:"+ JSON.toJSONString(sysDicItem));
        int i = sysDicItemMapper.deleteSysDicItem(sysDicItem);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("删除系统字典项失败");
        }
    }
}
